<!-- 右 -->
<div class="header">
    <h1 class="page-title">商品列表</h1>
</div>

<div class="well">
    <!-- search button -->
    <form action="{:url('admin/goods/index')}" method="get" class="form-search">
        <div class="row-fluid" style="text-align: left;">
            <div class="pull-left span4 unstyled">
                <p> 商品名称：<input class="input-medium" name="keyword" type="text" value="{$params}"></p>
            </div>
        </div>
        <button type="submit" class="btn">查找</button>
        <a class="btn btn-primary" href="{:url('admin/goods/create')}">新增</a>
        <button type="submit" class="btn" id="reload">刷新</button>
        <select id="page">
            <option value="0">下拉分页</option>
            <option value="3">3</option>
            <option value="5">5</option>
            <option value="10">10</option>
        </select>
    </form>
</div>
<div class="well">
    <!-- table -->
    <table class="table table-bordered table-hover table-condensed">
        <thead>
        <tr>
            <th>编号</th>
            <th>商品名称</th>
            <th>商品价格</th>
            <th>商品数量</th>
            <th>商品状态</th>
            <th>商品logo</th>
            <th>商品缩略图</th>
            <th>商品相册</th>
            <th>商品简介</th>
            <th>添加时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="tbody">
        {foreach $goods as $vo}
        <tr class="success">
            <td>{$vo['id']}</td>
            <td><a href="{:url('admin/goods/read',['id'=>$vo.id])}">{$vo.goods_name}</a></td>
            <td>{$vo.goods_price}</td>
            <td>{$vo.goods_number}</td>
            <td>
                {if $vo.is_on_sale == 1}
                <span onclick="isOnSale({$vo.id})" status="0" class="goods{$vo.id}">上架</span>
                {else/}
                <span onclick="isOnSale({$vo.id})" status="1" class="goods{$vo.id}">下架</span>
                {/if}
            </td>
            <td><img src="{$vo.goods_logo}"></td>
            <td><img src="{$vo.goods_thumb}"></td>
            <td>
                {foreach $vo.goods_pic as $pic}
                    <img src="{$pic}">
                {/foreach}
            </td>
            <td>{$vo.goods_desc}</td>
            <td>2017-04-01 08:00:00</td>
            <td>
                <a href="{:url('admin/goods/edit',['id'=>$vo.id])}"> 编辑 </a>
                <!--                <a href="javascript:void(0);" onclick="if(confirm('确认删除？')) location.href='admin/goods/delete?id={$vo.id}'"> 删除 </a>-->
                <a href="{:url('admin/goods/delete',['id'=>$vo.id])}">删除</a>
            </td>
        </tr>
        {/foreach}
        </tbody>
    </table>
    <!-- pagination -->
    {$goods->render()}
</div>
<script>
    $('#reload').click(function () {
        //刷新当前页面
        window.location.reload();
    })

    //修改上下架
    function isOnSale(id) {
        var status = $('.goods' + id).attr('status');
        var onSale = $('.goods' + id).text();

        if(onSale == '上架'){
            onSale = '下架'
            status = 1;
        }else{
            onSale = '上架'
            status = 0;
        }
        console.log(onSale+'----------'+status);
        //ajax
        $.ajax({
            'url': 'http://www.1906a.com/admin/goods/modifyGoodsStatus',
            'type': 'POST',
            'data': {
                'id': id,
                'status': status
            },
            'dataType': 'JSONP',//实现跨域
            'jsonp':'callback',
            'success': function (res) {
                console.log(res);
                if(res.code == 200){
                    //修改成功
                    $('.goods' + id).text(onSale);
                    $('.goods' + id).attr('status',status);
                }
            },
            'error': function (error) {
                console.log(error)
            },
        });
    }

    $('#page').change(function () {
        var page = $('#page option:selected').val();
        //ajax
        $.ajax({
            'url': '?page',
            'type': 'GET',
            'dataType': 'JSON',
            'success': function (res) {
                console.log(res);

                var data = res.data.data;
                var str = '';
                $.each(data, function (i, v) {
                    str += "        <tr class=\"success\">\n" +
                        "            <td>" + v.id + "</td>\n" +
                        "            <td><a href=\"{:url('admin/goods/read',['id'=>" + v.id + "])}\">" + v.goods_name + "</a></td>\n" +
                        "            <td>" + v.goods_price + "</td>\n" +
                        "            <td>" + v.goods_number + "</td>\n" +
                        "            <td><img src=\"" + v.goods_logo + "\"></td>\n" +
                        "            <td>" + v.goods_desc + "</td>\n" +
                        "            <td>2017-04-01 08:00:00</td>\n" +
                        "            <td>\n" +
                        "                <a href=\"{:url('admin/goods/edit',['id'=>" + v.id + "])}\"> 编辑 </a>\n" +
                        "<!--                <a href=\"javascript:void(0);\" onclick=\"if(confirm('确认删除？')) location.href='admin/goods/delete?id={$vo.id}'\"> 删除 </a>-->\n" +
                        "                <a href=\"{:url('admin/goods/delete',['id'=>" + v.id + "])}\" >删除</a>\n" +
                        "            </td>\n" +
                        "        </tr>";
                })

                $('#tbody').html(str);
            },
            'error': function (error) {
                console.log(error)
            },
        });
    })
</script>